﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using BaseballGame.Models;

namespace BaseballGame.Controllers
{
	public class GameEngine
	{
		public int ScoreHome = 0;
		public int ScoreAway = 0;
		private int Outs = 0;
		private int batterHome = 1;
		private int batterAway = 1;
		public int HitsHome = 0;
		public int HitsAway = 0;
		private bool b1 = false;
		private bool b2 = false;
		private bool b3 = false;

		public GameEngine(Team Home, Team Away)
		{
			int Inning = 1;
			Random random = new Random();



			while (Inning <= 9)
			{
				Bat(Home, Away, random, true);

				Outs = 0;

				Bat(Away, Home, random, false);

				Outs = 0;

				Inning++;
			}
		}

		int hit(int bases)
		{
			int score = 0;
			switch (bases)
			{
				case 1: //single
					if (b1 && b2 && b3)
					{
						score++;
					}
					else if (b1 && b2)
					{
						b3 = true;
					}
					else if (b1)
					{
						b2 = true;
					}
					else
					{
						b1 = true;
					}
					break;
				case 2: //double
					if (b1 && b2 && b3)
					{
						score++;
						score++;
						b1 = false;
					}
					else if (b1 && b2)
					{
						score++;
						b1 = false;
						b3 = true;
					}
					else if (b1)
					{
						b1 = false;
						b2 = true;
						b3 = true;
					}
					else
					{
						b2 = true;
					}
					break;
				case 3: //triple
					if (b1)
						score++;
					if (b2)
						score++;
					if (b3)
						score++;
					b3 = true;
					b1 = false;
					b2 = false;
					break;
				case 4: //HR
					if (b1)
						score++;
					if (b2)
						score++;
					if (b3)
						score++;
					score++;
					b1 = false;
					b2 = false;
					b3 = false;


					break;
				default:
					break;
			}
			return score;
		}
		/// <summary>
		/// jnbjn
		/// </summary>
		/// <param name="A"></param>
		/// <param name="B"></param>
		/// <param name="random"></param>
		/// <param name="Home"></param>
		void Bat(Team A, Team B, Random random, bool Home)
		{
			int plate = 0;
			int BattingTeam = 0;
			int hits = 0;
			int batter = 1;
			if (Home)
			{
				batter = batterHome;
			}
			else
			{
				batter = batterAway;
			}

			b1 = false;
			b2 = false;
			b3 = false;

			while (Outs <= 3)
			{

				switch (batter)
				{
					case 1:
						plate = HitOrOut(A.FirstBase, B.Pitcher, random);
						if (plate == 0)
						{
							Outs++;
						}
						else
						{
							hits++;
							BattingTeam = hit(plate);
						}
						break;
					case 2:
						plate = HitOrOut(A.SecondBase, B.Pitcher, random);
						if (plate == 0)
						{
							Outs++;
						}
						else
						{
							hits++;
							BattingTeam = hit(plate);
						}
						break;
					case 3:
						plate = HitOrOut(A.ThirdBase, B.Pitcher, random);
						if (plate == 0)
						{
							Outs++;
						}
						else
						{
							hits++;
							BattingTeam = hit(plate);
						}
						break;
					case 4:
						plate = HitOrOut(A.ShortStop, B.Pitcher, random);
						if (plate == 0)
						{
							Outs++;
						}
						else
						{
							hits++;
							BattingTeam = hit(plate);
						}
						break;
					case 5:
						plate = HitOrOut(A.Catcher, B.Pitcher, random);
						if (plate == 0)
						{
							Outs++;
						}
						else
						{
							hits++;
							BattingTeam = hit(plate);
						}
						break;
					case 6:
						plate = HitOrOut(A.LeftField, B.Pitcher, random);
						if (plate == 0)
						{
							Outs++;
						}
						else
						{
							hits++;
							BattingTeam = hit(plate);
						}
						break;
					case 7:
						plate = HitOrOut(A.Rightfield, B.Pitcher, random);
						if (plate == 0)
						{
							Outs++;
						}
						else
						{
							hits++;
							BattingTeam = hit(plate);
						}
						break;
					case 8:
						plate = HitOrOut(A.CenterField, B.Pitcher, random);
						if (plate == 0)
						{
							Outs++;
						}
						else
						{
							hits++;
							BattingTeam = hit(plate);
						}
						break;
					case 9:
						plate = HitOrOut(A.Pitcher, B.Pitcher, random);
						if (plate == 0)
						{
							Outs++;
						}
						else
						{
							hits++;
							BattingTeam = hit(plate);
						}
						break;
					default:

						break;
				}
				batter++;
				if (batter > 9)
					batter = 1;

			}

			if (Home)
			{
				ScoreHome += BattingTeam;
				batterHome = batter;
				HitsHome += hits;
			}
			else
			{
				ScoreAway += BattingTeam;
				batterAway = batter;
				HitsAway += hits;
			}
		}

		/// <summary>
		/// Return
		/// 0: Out,
		/// 1: Single,
		/// 2: Double,
		/// 3: Thriple,
		/// 4: HR,
		/// </summary>
		/// <param name="Batter"></param>
		/// <param name="Pitcher"></param>
		/// <returns></returns>
		int HitOrOut(Player Batter, Player Pitcher, Random random)
		{

			int randomNumber = random.Next(0, 100);
			int HittingChance = Batter.hitting_chance;
			int PitchingChance = Pitcher.pitching;
			int TotalChance = HittingChance - PitchingChance;

			if (randomNumber > TotalChance)
			{
				return 0;
			}
			else
			{
				randomNumber = random.Next(0, 100);
				if (randomNumber > Batter.power * 8)
				{
					return 4;
				}
				else if (randomNumber > Batter.power * 5)
				{
					return 3;
				}
				else if (randomNumber > Batter.power * 3)
				{
					return 2;
				}
				else
				{
					return 1;
				}
			}
		}
	}
}